import requests
from requests.exceptions import ReadTimeout, ConnectionError, RequestException
from bs4 import BeautifulSoup as Bs


def parse_html_data(response):
    # print(type(response.content.decode('utf-8')))
    html_doc = response.content.decode('utf-8')
    soup = Bs(html_doc, 'html.parser')
    '''find id'''
    print(soup.prettify())


def main():
    # https://www.jianshu.com/c/5AUzod?order_by=commented_at&_pjax=%23list-container
    # https://www.jianshu.com/c/5AUzod?order_by=commented_at&page=1
    # url_string = r'http://www.baidu.com/index.html'
    url_string = r'https://www.jianshu.com/c/5AUzod?order_by=commented_at&page=1'
    header = {
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6)",
        "Host": "www.jianshu.com"
    }
    try:
        with requests.get(url_string, headers=header) as f:
            if not f.status_code == requests.codes.ok:
                print(f.status_code)
            else:
                parse_html_data(f)
    except ReadTimeout:
        print('Time Out')
    except ConnectionError:
        print('Connection Error')
    except RequestException:
        print('Request Error')


if __name__ == '__main__':
    main()
